<?php
namespace app\index\controller;

use app\BaseController;
use think\facade\Session;

/**
 * 前端公共控制器
 */
class Common extends BaseController
{
    // 用户信息
    public $userId;

    // 请求参数
    protected $params;

    /**
     * 构造方法
     */
    public function __construct()
    {
        $this->userId = Session::get("id");
    }

    /**
     * 成功提示
     * @desc    description
     * @param   [string]      $msg [提示信息、默认（操作成功）]
     */
    public function success($msg)
    {
        if(IS_AJAX)
        {
            return DataReturn($msg, 0);
        } else {
            MyViewAssign('msg', $msg);
            return MyView('public/jump_success');
        }
    }

    /**
     * 错误提示
     * @desc    description
     * @param   [string]      $msg [提示信息、默认（操作失败）]
     */
    public function error($msg)
    {
        if(IS_AJAX)
        {
            return DataReturn($msg, -1);
        } else {
            MyViewAssign('msg', $msg);
            return MyView('public/jump_error');
        }
    }

    /**
     * 空方法响应
     * @desc    description
     * @param   [string]         $method [方法名称]
     * @param   [array]          $args   [参数]
     */
    public function __call($method, $args)
    {
        if(IS_AJAX)
        {
            return DataReturn($method.' 非法访问', -1000);
        } else {
            MyViewAssign('msg', $method.' 非法访问');
            return MyView('public/tips_error');
        }
    }

    /**
     * 转换类型为字符串
     */
    public function toString($str)
    {
        if (is_array($str)) {
            foreach ($str as $k => $v) {
                $str[$k] = $this->toString($v);
            }
        } else {
            $str = (string)$str;
        }
        return $str;
    }

    /**
     * 对象转换数组
     * @param $obj
     * @return array|void
     */
    function object_to_array($obj)
    {
        $obj = (array)$obj;
        foreach ($obj as $k => $v) {
            if (gettype($v) == 'resource') {
                return;
            }
            if (gettype($v) == 'object' || gettype($v) == 'array') {
                $obj[$k] = (array)$this->object_to_array($v);
            }
        }
        return $obj;
    }

    /**
     * 接口返回json格式数据
     * @param int $status 返回状码
     * @param string $msg 提示内容
     * @param type $arr 要返回的数据
     */
    protected function showJson($status = 0, $msg = '', $array = null)
    {
        $data = array();
        $data['code'] = $status;
        $data['msg'] = $msg;
        if (is_array($array)) {
            $data['data'] = $array;
        }
        exit(json_encode($data));
    }

    /**
     * 公共配置信息
     * @desc    description
     * @param   [array]           $params [输入参数]
     */
    public static function common()
    {
        // 配置信息
        $config = [
            //送货方式
            'delivers' => [
                [
                    'id' => 1,
                    'name' => '普通送货方式',
                    'price' => 3,
                    'info' => '非常慢',
                ],
                [
                    'id' => 2,
                    'name' => '顺丰',
                    'price' => 10,
                    'info' => '非常快',
                ],
                [
                    'id' => 3,
                    'name' => '菜鸟',
                    'price' => 5,
                    'info' => '比较慢',
                ],
            ],
            'payType' => [
                [
                    'id' => 1,
                    'name' => '在线支付',
                    'info' => '即时到帐，支持绝大数银行借记卡及部分银行信用卡'
                ],
                [
                    'id' => 2,
                    'name' => '微信支付',
                    'info' => '微信支付，支持绝大数银行借记卡及部分银行信用卡'
                ],
                [
                    'id' => 3,
                    'name' => '货到付款',
                    'info' => '货到付款，支持绝大数银行借记卡及部分银行信用卡'
                ],
            ],
        ];
        return $config;
    }

    public function generateTree($array){
        //第一步 构造数据
        $items = array();
        foreach($array as $value){
            $items[$value['id']] = $value;
        }
        //第二部 遍历数据 生成树状结构
        $tree = array();
        foreach($items as $key => $value){
            if(isset($items[$value['pid']])){
                $items[$value['pid']]['children'] = array();
                $items[$value['pid']]['children'][] = $items[$key];
            }else{
                $tree[] = &$items[$key];
            }
        }
        return $tree;
    }

    public function getNavData($allData)
    {
        $return = array();
        foreach ($allData as $k => $v)
        {
            if ($v['pid'] == 0)
            {
                if (!isset($v['child'])){
                    $v['child'] = array();
                }
                foreach ($allData as $k1 => $v1)
                {
                    if ($v1['pid'] == $v['id'])
                    {
                        if (!isset($v1['child']))
                        {
                            $v1['child'] = array();
                        }
                        foreach ($allData as $k2 => $v2)
                        {
                            if ($v2['pid'] == $v1['id'])
                            {

                                $v1['child'][] = $v2;
                            }
                        }
                        $v['child'][] = $v1;
                    }
                }
                $return[] = $v;
            }
        }
        return $return;
    }
}
?>